var orderId="";
var startDate=echartUtil.GetDateTimeStr(0); 
//相关业务逻辑
$(function(){ 
	//初始化所有下拉
   material.initSelect(0);
	//material.initSelect(0,null);
   page.bind();
   material.bind();
   $("#warehouseOutTime").val(startDate);
}); 

var table;
var page={//页面总变量
		bind:function(){
			$("#queryFedd").unbind("click");
			$("#orderId").unbind("kepup");
			$("#outWarehouse").unbind("click");
			
			//查询投料单
			$("#queryFedd").bind("click",function(){
				var object = page.getSendParam();
				page.getFeedOrderMaterialDate(object);
				$(".allChecked").show(); 
			});
			
			//输入订单编号
			$("#orderId").bind("keyup",function(){
				var orderId=$(this).val();
				$.ajax({
					url:'weiXin/getOrderDetail',
					data:{
						'orderId':orderId
					},
					success:function(data){
						if(data.result){
							data=data.data;
							var html=""
						    for(var i=0;i<data.length;i++){
						    	html+="<option value='"+data[i].orderId+"'>"+data[i].companyOrderId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].factoryId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerOrderId+"&nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerId+"</option>"
						    }
							$("#detailList").html(html);
						}
					}
				});
			});
			
			 //确认出库
			 $("#outWarehouse").bind("click",function(){
				 page.checkOutWarehouse();
			 });
			 //全选与取消 
			 $(".allChecked").bind("click",function(){
				 var tr = $("#inWarehuseTable").find("tbody tr");
				 if(tr.length>0){
					 if(tr.eq(0).find("td input[name='checkbox']").length>0){//说明有查出数据
						 var val = $(this).text();
						 var len = tr.length;
						 if(val==='全选'){
							 for(var i=0;i<len;i++){
								 tr.eq(i).find("td input[name='checkbox']").prop("checked",true);
							 }
							 $(this).text("取消"); 
						 }else{ 
							 for(var i=0;i<len;i++){
								 tr.eq(i).find("td input[name='checkbox']").prop("checked",false);
							 }
							 $(this).text("全选");
						 }
					 }
				 }
			 });
		}, 
		getSendParam:function(){
			var orderId=$("#orderId").val().trim();
			var factoryNo = $("#factoryNo").val().trim();
			var customerNo = $("#customerNo").val().trim();
			var warehouse = $("select[name=warehuose]").val();
			var object = new Object();
			object.warehouse = warehouse; 
			object.orderId = orderId;//订单编号
			object.factoryNo = factoryNo;//工厂型号
			object.customerNo = customerNo;//客户型号
			object.materialName = $("#materialName").filterSelect("getValue");
			object.model = $("#model").filterSelect("getValue");
			object.color = $("#color").filterSelect("getValue");
			if(object.color==="全部" || object.color===""){
				object.color = 'all';
			}
			if(object.warehouse==="0"){ 
				layAlert("请选择仓库");
				return false;
			}
			if(object.materialName==='全部'){
				object.materialName = '';
			}
			if(object.model==='全部'){
				object.model = '';
			}
			if(object.color==='全部'){
				object.color = '';
			}
			//通过订单信息查询 
			if((object.orderId!==null && object.orderId.trim()!=='')|| (object.factoryNo!==null && object.factoryNo.trim()!== '') || (object.customerNo!==null && object.customerNo.trim() !== '')){
				object.queryType = "feedOrder"
			}else{//没有通过订单信息查询
				object.queryType = "material"
			}
			return object;
		},
		getFeedOrderMaterialDate:function(object){
			table=ajaxUtil.dataTablesAjaxPrintException("inWarehuseTable",8,"order/getFeedOrderMaterialDate",
					object,[ 
					{"data":"companyOrderId",render:function(data,type,row){ 
						return (data===null?"":data)+"<input type='hidden' value='"+(row.orderId===null?"":row.orderId)+"'/>";
					}},
					{"data":"factoryNo"},
					{"data":"articleNo",render:function(data,type,row){
						return (data===null?"":data)+"<input type='hidden' value='"+row.id+"' feedOrderId='"+row.feedOrderId+"' />"
					}}, 
					{"data":"materialName",render: function (data,type,row) {
						return (data===null?"":data)+"<input type='hidden' value='"+row.materialCode+"' />"
					}},
					/*{"data":"materialType",render: function (data,type,row) {
							return (data===null?"":data)+"<input type='hidden' value='"+row.id+"' feedOrderId='"+row.feedOrderId+"' />"
						}}, */
					{"data":"color",render:function(data,type,row){
						return data+"<input type='hidden' class='modal' value='"+row.modal+"' providerName='"+row.providerName+"'/>"
					}},
					{"data":"colorDescription"},
					{"data":"modal"},
					{"data":"feedCount"},
					{"data":"receiveCount",render: function (data,type,row) {
						if(row.orderId===null){
							return "<input type='text' class='input-text receiveCount' value='"+0+"' />";
						}else{
							return "<input type='text' class='input-text receiveCount' value='"+0+"' />";
						} 
					}},
					{"data":"productionUnitFeedCount",render: function (data,type,row) {
						if(row.orderId===null){
							return "无";
						}else{
							return data;
						} 
					}},
					{"data":"productionUnitReceiveCount",render: function (data,type,row) {
						if(row.orderId===null){
							//return "<input type='text' class='input-text productionUnitReceiveCount' value='"+0+"' />";
							return "无";
						}else{
							return "<input type='text' class='input-text productionUnitReceiveCount' value='"+0+"' />";
						} 
					}},
					{"data":"warehouseCount",render:function(data,type,row){
						return (data<0?0:data);  
					}}, 
					{"data":"productionUnitCount",render:function(data,type,row){
						if(row.orderId===null){
							return "无";
						}else{
							return (data<0?0:data);
						}   
					}},
					{"data":"warehouseCount",render:function(data,type,row){
						return "<input type='text' class='input-text remark' value=''/>";
					}},
					{"data":"id",render:function(data,type,row){
						return "<input type='checkbox' name='checkbox' id='"+data+"' />";  
					}}
			]);
		},
		checkOutWarehouse:function(){
			 var orderId=$("#orderId").val();
			 var object=new Object();
			 var scheduledWarehouseOutTime = $("#warehouseOutTime").val();//领料时间
			 object.feedOrder=orderId;//领取的投料单
			 object.scheduledWarehouseOutTime=scheduledWarehouseOutTime;//预计领料时间
			 object.type=2;//领料单出库
			 if(object.warehouseOutTime==''){
				 layAlert("请选择预计领料时间");
				 return false;
			 }
			 var metailArray=new Array();
			 //原料仓出库接口
			 var requestUrl = "companyMaterial/savdOutWarehouse";
			 var warehouse = $("select[name=warehuose]").val();
			 if(warehouse==1){//原料仓出库 
				 if(orderId===null ||orderId.trim()===''){//类型五出库
					 $('#inWarehuseTable input[name="checkbox"]:checked').each(function(){
						 var dataCopy=jQuery.extend({}, object);
						 var $tr = $(this).parents("tr");
						 var orderVal = $tr.find("td").eq(0).find("input").val();
						 if(orderVal==null){
							 orderVal=''
						 }
						 if(orderVal!==null && orderVal.trim()!==''){
							 layAlert("未输入订单编号,无法生成有订单编号的领料单");
							 return false;
						 } 
						 //物料单位领料数
						 var outCount=$tr.children("td").eq(8).children("input").val().trim();
						 //物料单位库存数
						 var count=$tr.children("td").eq(11).text();
						 if(isNaN(Number(outCount)) || outCount===""){
							 layAlert("物料单位领料数必须是数字");
							 return; 
						 }
						 if(Number(outCount)<0){
							 layAlert("物料单位领料数必须大于0");
							 return;
						 }
						 if(Number(outCount)>Number(count)){
							 layAlert("物料单位领料数不能大于物料单位库存数");
							 return false;
						 }
						 if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(outCount)) {
								layAlert("物料单位领料数量小数点后只能是0-2位"); 
								return false; 
						 }
						 //物料单位出库
						 if(outCount>0){ 
							 dataCopy.count=outCount;
							 dataCopy.materialCode=$tr.children("td").eq(3).children("input").val();
							 dataCopy.materialName=$tr.children("td").eq(3).text();
							 dataCopy.color = $tr.children("td").eq(4).text();
							 dataCopy.model = $tr.children("td").eq(6).text()
							 dataCopy.providerName = $tr.children("td").eq(4).children("input").attr("providerName");
							 dataCopy.feedOrder=null;
							 dataCopy.type = 5;//领料单出库 
							 dataCopy.remark = $tr.children("td").eq(13).children("input").val();
							 dataCopy.isProductionUnit = 0;
							 metailArray.push(dataCopy);
						 } 
						 
					 });
				 }else{//有订单编号,类型二出库
					 $('#inWarehuseTable input[name="checkbox"]:checked').each(function(){
						
						 var $tr = $(this).parents("tr");
						 var orderVal = $tr.find("td").eq(0).find("input").val();
						 var companyOrderId = $tr.find("td").eq(0).text();
						 var orderInput = $("#orderId").val().trim(); 
						 if(orderVal!==orderInput && companyOrderId!==orderInput){
							 layAlert("输入的订单编号与要保存的物料订单编号不符,无法保存");
							 return false;
						 }  
						 //物料单位领料数
						 var outCount=$tr.children("td").eq(8).children("input").val().trim();
						 //物料库存数
						 var count=$tr.children("td").eq(11).text();
						 //生产单位库存数
						 var productionUnitCount = $tr.children("td").eq(12).text();
						 //物料单位投料数
						 var feedCount=$tr.children("td").eq(7).text();
						 //生产单位领料数
						 var productionUnitOutCount=$tr.children("td").eq(10).children("input").val().trim();
						 //生产单位投料数 
						 var productionUnitFeedCount = $tr.children("td").eq(9).text();
//						 if(isNaN(Number(outCount)) || outCount===""){
//							 layAlert("物料单位领料数必须是数字"); 
//							 return;
//						 }  
						 //生产单位或者物料单位
						 if((isNaN(Number(productionUnitOutCount)) || productionUnitOutCount==="") && (isNaN(Number(outCount)) || outCount==="")){
							 layAlert("生产单位领料数或生产单位领料数必须是数字"); 
							 return;
						 } 
						 if(Number(outCount)<0 || Number(productionUnitOutCount)<0){
							 layAlert("物料单位领料数或生产单位领料数必须大于0");
							 return;
						 }
						 if(Number(outCount)>Number(count)){
							 layAlert("物料单位领料数不能大于物料单位库存数");
							 return false;
						 }
						 if(Number(outCount)>Number(feedCount)){
							 layAlert("物料单位领料数不能大于物料单位投料数"); 
							 return false;
						 }
						 if(Number(productionUnitOutCount)>Number(productionUnitCount)){
							 layAlert("生产单位领料数不能大于生产单位库存数");
							 return false;
						 }
						 if(Number(productionUnitOutCount)>Number(productionUnitFeedCount)){
							 layAlert("生产单位领料数不能大于生产投料数"); 
							 return false;
						 } 
						 if (!/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(outCount) && !/^(0|[1-9]\d*)(\.\d{0,2})?$/.test(productionUnitOutCount)) {
								layAlert("物料单位领料数量或者生产单位领料数量小数点后只能是0-2位"); 
								return false; 
						 } 
						 //物料单位出库 
						 if(outCount>0){ 
							 var dataCopy=jQuery.extend({}, object);
							 //物料单位出库数
							 dataCopy.count=Number(outCount);
							 //生产单位出库数
							 dataCopy.materialCode=$tr.children("td").eq(3).children("input").val();
							 dataCopy.materialName=$tr.children("td").eq(3).text();
							 dataCopy.color = $tr.children("td").eq(4).text();
							 dataCopy.model = $tr.children("td").eq(6).text()
							 dataCopy.providerName = $tr.children("td").eq(4).children("input").attr("providerName");
							 dataCopy.feedOrder=$tr.children("td").eq(2).children("input").val();
							 dataCopy.remark = $tr.children("td").eq(13).children("input").val();
							 dataCopy.isProductionUnit = 0;
							 metailArray.push(dataCopy);
						 }
						 //生产单位出库
						 if(productionUnitOutCount>0){ 
							 var dataCopy=jQuery.extend({}, object);
							 //生产单位出库数
							 dataCopy.count = Number(productionUnitOutCount);
							 dataCopy.materialCode=$tr.children("td").eq(3).children("input").val();
							 dataCopy.materialName=$tr.children("td").eq(3).text();
							 dataCopy.color = $tr.children("td").eq(4).text();
							 dataCopy.model = $tr.children("td").eq(6).text()
							 dataCopy.providerName = $tr.children("td").eq(4).children("input").attr("providerName");
							 dataCopy.feedOrder=$tr.children("td").eq(2).children("input").val();
							 dataCopy.remark = $tr.children("td").eq(13).children("input").val();
							 dataCopy.isProductionUnit = 1;
							 metailArray.push(dataCopy);
						 }
					 });
				 }
				 //其他仓库出库
			 }else if(warehouse==2 || warehouse==3){
				 layAlert("无该类型出库");
				 return false;
			 }
			 if(metailArray.length<=0){
				 return false;
			 } 
			 ajaxUtil.ajaxRuquest(requestUrl,{"metailArray":JSON.stringify(metailArray)},function(data){
				 layer.msg(data,{icon:1}); 
				 material.initSelect(0,null); 
				 $("select[name=warehuose]").val(0);  
				 //$("#inWarehuseTable").find("tbody").html("");
				 var object = page.getSendParam();
				 page.getFeedOrderMaterialDate(object);
			 });
		} 
		
}
//物料对象
var material = {
		//绑定方法 
		bind:function(){ 
			$("#materialName").bind("change",function(){
				var object=new Object(); 
				object.materialName=$(this).filterSelect("getValue");
				//如果物料名称选择的是'请选择' 那么就全部初始化
				if(object.materialName==='' ||object.materialName==='全部'){
					object.materialName = '';
					material.initSelect(1,object); 
				}else{
					material.getSelectVal("basicData/getModelByCountAndMaterialName",object,"model");
				} 
			});  
			$("#model").bind("change",function(){
				var object=new Object();
				object.materialName=$("#materialName").filterSelect("getValue");
				object.model=$("#model").filterSelect("getValue");
				if(object.materialName==='' || object.materialName==='全部'){
					object.materialName = '';
				}  
				//如果物料名称或规格选择的是'请选择' 那么就根据有效值初始化
				if(object.model==='' || object.model==='全部'){
					object.model = '';
					material.initSelect(2,object);
				}else{  
					material.getSelectVal("basicData/getColorByCountAndMaterialName",object,"color");
				}
			}); 
			
		},
		initSelect:function(index,object){
			if(index==0){//查全部
				this.getSelectVal("basicData/getCompanyMaterialNameByCount",object,"materialName");
				this.getSelectVal("basicData/getModelByCountAndMaterialName",object,"model");
				this.getSelectVal("basicData/getColorByCountAndMaterialName",object,"color");
				$("#orderId").val("");
				$("#factoryNo").val("");
				$("#customerNo").val("");
			} 
			if(index==1){//通过物料名称查下面所有
				this.getSelectVal("basicData/getModelByCountAndMaterialName",object,"model");
				this.getSelectVal("basicData/getColorByCountAndMaterialName",object,"color");
			}
			if(index==2){//通过物料规格查下面所有
				this.getSelectVal("basicData/getColorByCountAndMaterialName",object,"color");
			}
		},
		//请求数据
		getSelectVal:function(url,sendData,domId){
			ajaxUtil.ajaxRuquest(url,sendData,function(data){
				if(data!=null && data.length>0){
					data.unshift("全部"); 
					var option = {
							data:_.map(data,function(d){
								if(d===null){
									d = '';
								}
								return {
									id:d,
									name:d
								} 
							}),
							valueField:"id", 
							labelField:"name" 
					}  
					$("#"+domId).filterSelect("destroy");
					$("#"+domId).filterSelect(option);
				}
			});
		}
}
